<?php

class Application_Model_DbTable_Category extends Zend_Db_Table_Abstract
{

    protected $_name = 'Category';

    public function addCategory($name,$description){
        $data = array(

            "name" => $name,
            "details" => $description

            );
        $this->insert($data);
    }

  public function getCategory($id){

      $query = $this->select();
        $query->from(array('c'=>'Category'));
        $query->where("category_id = ".$id);
        $query->setIntegrityCheck(false);
        $category = $this->fetchRow($query);
        return $category->toArray();
  }

  public function getIdCategory($name){
        $row = $this->fetchRow("name = '".$name."'");
        if(!$row) return -1;
        else return $row->category_id;
    }


    public function deleteCategory($categoryID){
        $this->delete("category_id =".(int)$categoryID);
    }

    public function getNameCategory($ID){
        $row = $this->fetchRow("category_id = ".(int)$ID);
        return $row["name"];
    }

    public function getShops($id){
        $query = $this->select();
        $query->from(array('c'=>'Category'));
        $query->joinInner(array('st'=>'Shop_type'), 'st.category_id = c.category_id', array());
        $query->joinInner(array('s'=>'Shop'), 'st.shop_id = s.shop_id');
        $query->setIntegrityCheck(false);
        $query->where("c.category_id = $id AND s.deleted = 0");
        return $this->fetchAll($query);
    }

    public function getDetails($id){

        $row = $this->fetchRow("category_id = ". (int)$id);
        return $row["details"];

    }
    
    public function updateCategory($category_id,$name,$details){

        $category = array(
            "name" => $name,
            "details" => $details
        );

        $this->update($category, "category_id =".(int)$category_id);
        //$this->insert($category);

    }

  

    
}

